package com.swordget.controller;

import com.swordget.entity.TemplateDesign;
import com.swordget.entity.vo.*;
import com.swordget.service.TemplateManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: David
 * @Date: Created in 15:23 2020/5/12
 * @Description: 模板管理控制器类
 * @Modified By:
 * @Version:
 */

@RestController
@RequestMapping("/templateManager")
public class TemplateManagerController {

    private TemplateManagerService tmpMagService;


    @Autowired
    public TemplateManagerController(TemplateManagerService tmpMagService) {
        this.tmpMagService = tmpMagService;
    }

    /**
     * 添加模板
     * @param requestVo
     * @return
     */
    @PostMapping
    public SimpleResponseVo addTemplate(@RequestBody SimpleRequestVo<TemplateVo> requestVo) {
        boolean isAdd = tmpMagService.addTemplate(requestVo);
        if (isAdd) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }


    /**
     * 查询表对应的全部模板信息
     * @param pageRequestVo
     * @return
     */
    @GetMapping
    public SimplePageResponseVo<List<TemplateDesign>> findAll(SimplePageRequestVo pageRequestVo) {
        List<TemplateDesign> templateDesigns =  tmpMagService.findAll(pageRequestVo);
        return SimplePageResponseVo.<List<TemplateDesign>>builder()
                .rows(templateDesigns)
                .total(templateDesigns.size())
                .build();
    }


    /**
     * 删除模板
     * @param serialNo
     * @return
     */
    @DeleteMapping
    public SimpleResponseVo remove(String serialNo) {
        boolean isRemove = tmpMagService.remove(serialNo);
        if (isRemove) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 设置为默认
     * @return
     */
    @PutMapping
    public SimpleResponseVo setDefault(String serialNo, String type) {
        boolean isSet = tmpMagService.setDefault(serialNo, type);
        if (isSet) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }


}
